#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <pthread.h>
#include <hiredis/hiredis.h>
#include "idc_ubus_mqtt.h"
#include "crc.h"
#include "frame_util.h"
#include "protocol.h"
#include "mqtt_service.h"
#include "data_type.h"
#include <time.h>
#include <semaphore.h>
#include "hash_map.h"
extern t_RunCfg runCfg;
extern HashMap *config_devs;

union float_u32_converter {
    uint32_t u32;
    float f;
};

char* double_to_string_fixed(double value) {
    int length = snprintf(NULL, 0, "%.3f", value) + 1;
    char* buffer = (char*)malloc(length);
    snprintf(buffer, length, "%.3f", value);
    return buffer;
}

/**
 * 发送电源信息数据到MQTT服务器
 */ 
int energy_info(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg)
{
        struct blob_attr *tb[24];
        blobmsg_parse(enerty_info_policy, 24, tb, blob_data(msg), blob_len(msg));
        blob_buf_init(&b, 0);
        //准备json数据
        cJSON *root = cJSON_CreateObject();
        
        if(tb[0]){
                int addr = blobmsg_get_u32(tb[0]);
                EP_JSON_SET_INT(root, "loraId", addr);
                char lora_addr[5] = {0};
                sprintf(lora_addr, "%04X", addr);
                char *sn = map_get(config_devs, lora_addr);
                if(sn == NULL){
                        return -1;
                }
                EP_JSON_SET_STR(root, "sn", sn);
        }
        EP_JSON_SET_INT(root, "dataType", 1);
        EP_JSON_SET_INT(root, "version", 1);
        cJSON *data = cJSON_CreateObject();
        if(tb[4]){
                union float_u32_converter converter;
                converter.u32 = blobmsg_get_u32(tb[4]);
                char strFloat[10] = {0};
                sprintf(strFloat,"%.2f", (double)converter.f);
                EP_JSON_SET_FLOAT(data, "batteryVoltage", strFloat);
        }
        if(tb[5]){
                union float_u32_converter converter;
                converter.u32 = blobmsg_get_u32(tb[5]);
                char strFloat[10] = {0};
                sprintf(strFloat,"%.2f", (double)converter.f);
                EP_JSON_SET_FLOAT(data, "batteryElectric", strFloat);
        }
        if(tb[6]){
                int batteryCapacity = blobmsg_get_u32(tb[6]);
                EP_JSON_SET_INT(data, "batteryCapacity", batteryCapacity);
        }
        if(tb[7]){
                int batteryStatus = blobmsg_get_u32(tb[7]);
                EP_JSON_SET_INT(data, "batteryStatus", batteryStatus);
        }
        if(tb[8]){
                union float_u32_converter converter;
                converter.u32 = blobmsg_get_u32(tb[8]);
                char strFloat[10] = {0};
                sprintf(strFloat,"%.2f", (double)converter.f);
                EP_JSON_SET_FLOAT(data, "loadVoltage", strFloat);
        }
        if(tb[9]){
                union float_u32_converter converter;
                converter.u32 = blobmsg_get_u32(tb[9]);
                char strFloat[10] = {0};
                sprintf(strFloat,"%.2f", (double)converter.f);
                EP_JSON_SET_FLOAT(data, "loadElectric", strFloat);
        }
        if(tb[10]){
                union float_u32_converter converter;
                converter.u32 = blobmsg_get_u32(tb[10]);
                char strFloat[10] = {0};
                sprintf(strFloat,"%.2f", (double)converter.f);
                EP_JSON_SET_FLOAT(data, "loadPower", strFloat);
        }
        if(tb[11]){
                int loadStatus = blobmsg_get_u32(tb[11]);
                EP_JSON_SET_INT(data, "loadStatus", loadStatus);
        }
        if(tb[12]){
                union float_u32_converter converter;
                converter.u32 = blobmsg_get_u32(tb[12]);
                char strFloat[10] = {0};
                sprintf(strFloat,"%.2f", (double)converter.f);
                EP_JSON_SET_FLOAT(data, "columnVoltage", strFloat);
        }
        if(tb[13]){
                int columnStatus = blobmsg_get_u32(tb[13]);
                EP_JSON_SET_INT(data, "columnStatus", columnStatus);
        }
        if(tb[14]){
                int runDay = blobmsg_get_u32(tb[14]);
                EP_JSON_SET_INT(data, "runDay", runDay);
        }
        if(tb[15]){
                int lowVolCnt = blobmsg_get_u32(tb[15]);
                EP_JSON_SET_INT(data, "lowVolCnt", lowVolCnt);
        }
        if(tb[16]){
                int todayDischarge = blobmsg_get_u32(tb[16]);
                EP_JSON_SET_INT(data, "todayDischarge", todayDischarge);
        }
        if(tb[17]){
                int yesterdayDischarge = blobmsg_get_u32(tb[17]);
                EP_JSON_SET_INT(data, "yesterdayDischarge", yesterdayDischarge);
        }
        if(tb[18]){
                int totalDischarge = blobmsg_get_u32(tb[18]);
                EP_JSON_SET_INT(data, "totalDischarge", totalDischarge);
        }
        if(tb[19]){
                int todayCharge = blobmsg_get_u32(tb[19]);
                EP_JSON_SET_INT(data, "todayCharge", todayCharge);
        }
        if(tb[20]){
                int yesterdayCharge = blobmsg_get_u32(tb[20]);
                EP_JSON_SET_INT(data, "yesterdayCharge", yesterdayCharge);
        }
        if(tb[21]){
                int totalCharge = blobmsg_get_u32(tb[21]);
                EP_JSON_SET_INT(data, "totalCharge", totalCharge);
        }
        if(tb[22]){
                union float_u32_converter converter;
                converter.u32 = blobmsg_get_u32(tb[22]);
                char strFloat[10] = {0};
                sprintf(strFloat,"%.2f", (double)converter.f);
                EP_JSON_SET_FLOAT(data, "temperature", strFloat);
        }

        cJSON_AddItemToObject(root,"data",data);
        char *str = cJSON_Print(root);
        printf("%s\n", str);
        mqtt_send_data(str);
        cJSON_Delete(root);
        return 0;
} 


int send(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg)
{
        struct blob_attr *tb[1];
        blobmsg_parse(send_policy, 1, tb, blob_data(msg), blob_len(msg));
        blob_buf_init(&b, 0);
        if(tb[0]){
                char *json = blobmsg_get_string(tb[0]);
                printf("send data:%s\n", json);
                mqtt_send_data(json);
        }
        return 0;
}

